package com.learning.platform.repository;

import com.learning.platform.entity.Lesson;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LessonRepository extends JpaRepository<Lesson, Long> {
    
    /**
     * 根据课程ID查找所有章节（按顺序排列）
     */
    List<Lesson> findByCourseIdOrderByOrderNoAsc(Long courseId);
    
    /**
     * 获取课程的下一个排序号
     */
    @Query("SELECT COALESCE(MAX(l.orderNo), 0) + 1 FROM Lesson l WHERE l.courseId = :courseId")
    Integer getNextOrderNo(@Param("courseId") Long courseId);
    
    /**
     * 统计课程的章节数量
     */
    long countByCourseId(Long courseId);
    
    /**
     * 删除课程的所有章节
     */
    void deleteByCourseId(Long courseId);
}











